package com.sdleyou.treepro.common.service.impl;

import com.sdleyou.treepro.common.model.convert.CompanyDO2CompanyRankVOConverter;
import com.sdleyou.treepro.common.model.convert.UserDO2UserRankVO;
import com.sdleyou.treepro.common.model.dao.CompanyDO;
import com.sdleyou.treepro.common.model.dao.Page;
import com.sdleyou.treepro.common.model.dao.UserDO;
import com.sdleyou.treepro.common.model.vo.CompanyRankListVO;
import com.sdleyou.treepro.common.model.vo.CompanyRankVO;
import com.sdleyou.treepro.common.model.vo.UserRankListVO;
import com.sdleyou.treepro.common.model.vo.UserRankVO;
import com.sdleyou.treepro.common.service.MongoPageManager;
import com.sdleyou.treepro.common.service.UserRankManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserRankManagerImpl implements UserRankManager {
    @Autowired
    private MongoPageManager mongoPageManager;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public UserRankListVO userRank(Integer pageSize, Integer pageNum, String userId) {
        Query query = new Query();
        Criteria c = Criteria.where("nickname").exists(true);
        Sort sort = Sort.by(Sort.Direction.DESC, "total_grow");
        query.addCriteria(c).with(sort);
        Page<UserDO> page = mongoPageManager.pageQuery(query, UserDO.class, pageSize, pageNum);
        List<UserDO> userDOList = page.getData();
        List<UserRankVO> userRankVOList = userDOList.stream().map(e -> UserDO2UserRankVO.convert(e, userId)).collect(Collectors.toList());
        Query userQuery = new Query(Criteria.where("user_id").is(userId));
        UserDO userDO = mongoTemplate.findOne(userQuery, UserDO.class);
        //个人排名
        Query myQuery = new Query(Criteria.where("user_id").ne(userId).and("total_grow").gt(userDO.getTotalGrow()));
        long count = mongoTemplate.count(myQuery, UserDO.class);
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        map.put("user_name", userDO.getNickname());
        map.put("face", userDO.getFace());
        map.put("rank", count + 1);
        Integer myGrow = userDO.getTotalGrow() == null ? 0 : userDO.getTotalGrow();
        map.put("total_count", UserDO2UserRankVO.formatValue(myGrow));
        UserRankListVO listVO = new UserRankListVO();
        listVO.setRankList(new Page<>(page.getPageNo(), page.getDataTotal(), page.getPageSize(), userRankVOList));
        listVO.setMyRank(map);

        return listVO;
    }

    @Override
    public CompanyRankListVO companyRank(Integer pageSize, Integer pageNum, String companyId) {
        Query query = new Query(Criteria.where("company_id").ne(""));
        Sort sort = Sort.by(Sort.Direction.DESC, "tree_count");
        query.with(sort);
        Page<CompanyDO> page = mongoPageManager.pageQuery(query, CompanyDO.class, pageSize, pageNum);
        List<CompanyDO> companyDOList = page.getData();
        List<CompanyRankVO> companyRankVOList = companyDOList.stream().map(e -> CompanyDO2CompanyRankVOConverter.convert(e, companyId)).collect(Collectors.toList());
        Query companyQuery = new Query(Criteria.where("company_id").is(companyId));
        CompanyDO companyDO = mongoTemplate.findOne(companyQuery, CompanyDO.class);
        //公司排名
        Query myQuery = new Query(Criteria.where("company_id").ne(companyId).and("tree_count").gt(companyDO.getTreeCount()));
        long count = mongoTemplate.count(myQuery, CompanyDO.class);
        Map<String, Object> map = new HashMap<>();
        map.put("company_id", companyId);
        map.put("company_name", companyDO.getCompanyName());
        map.put("face", companyDO.getCompanyIcon());
        map.put("rank", count + 1);
        map.put("total_count", companyDO.getTreeCount());
        CompanyRankListVO listVO = new CompanyRankListVO();
        listVO.setRankList(new Page<>(page.getPageNo(), page.getDataTotal(), page.getPageSize(), companyRankVOList));
        listVO.setMyRank(map);

        return listVO;
    }
}
